package gui;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JLabel;
import javax.swing.JPanel;

import toolkit.Paint;
import data.SeatInterface;

public class Seat extends JLabel
{
	private int width = 80, height = 80, border = 5;
	
	private SeatInterface data;
	
	private boolean turn = false;
	
	public Seat()
	{
		setPreferredSize(new Dimension(width, height));
		setLayout(new BorderLayout());
		setHorizontalAlignment(CENTER);
	}
	
	public void setTurn(boolean turn)
	{
		this.turn = turn;
	}
	
	public boolean hasTurn()
	{
		return turn;
	}
	
	public boolean isTaken()
	{
		if (data != null)
		{
			return data.isTaken();
		}
		
		return false;
	}
	
	public void updateData(SeatInterface data)
	{
		this.data = data;
		
		String text = "<html>";
		
		text += "<b>" + data.getUsername() + "</b><br>";
		text += data.getBet() + "/" + data.getStack() + "<br>";
		
		if (data.getChosenAction() != null)
		{
			text += data.getChosenAction().getAction() + "<br>";
		}
		
		text += "</html>";
		
		setText(text);
	}
	
	public void paintComponent(Graphics g)
	{
		Paint paint = new Paint(g);
		
		Graphics2D g2 = paint.newLayer();
		
		if (!turn)
		{
			paint.setTransparancy(g2, 40);
		}
		
		Insets insets = getInsets();
		
		int width = getWidth() - insets.left - insets.right - border * 2;
		int height = getHeight() - insets.left - insets.right - border * 2;
		
		Ellipse2D seat = new Ellipse2D.Double(border, border, width, height);
		
		Graphics2D background = paint.newLayer();
		background.setColor(Color.WHITE);
		background.fill(seat);

		SeatState state = SeatState.FREE;
		
		if (data != null)
		{
			state = data.getState();
		}
		
		g2.setColor(state.toColor());
		g2.fill(seat);
		g2.setColor(Color.BLACK);
		g2.setStroke(new BasicStroke(5));
		g2.draw(seat);
		
		super.paintComponent(g);
	}
}
